%
% main.m
%
%==========================================================================

% PREPARE ENVIRONMENT
addpath( genpath('../util') );
preparePath();

% USER-DEFINED PARAMETERS -------------------------------------------------
kTRESHOLD = 0.0; % deformations with abs value less then treshold are not considered

% SELECT THE TYPE OF DATA
kTYPE = dataType.kHorse;
%kTYPE = dataType.kCamel;
%kTYPE = dataType.kFemale;
%kTYPE = dataType.kCloth;
%kTYPE = dataType.kFace;

% Control color map shift
kCOLOR_SLIDER = -2; % ( -inf... 0 ... +inf )
% ALPHA, BETA - parameters to make a weighted sum of strain and bending
% fields
kALPHA = 0.8;
kBETA = 1 - kALPHA;
% Neighborhood for local extremities search (feat points)
kDIJK_NEIGHBORHOOD = 0.05;
kNUMBER_OF_FRAMES = 48;
%dataName = 'camel';
dataName = 'horse';
% END USER-DEFINED PARAMETERS ---------------------------------------------


% COMPUTE STRAIN AND CURVATURE-CHANGE SCALAR FIELDS
%[averageStrain, averageBending, adjVertList, obj_ref, strainTemporal, bendingTemporal] = getScalarFields( kTYPE, kNUMBER_OF_FRAMES );

data = load( ['temporal-fields-' dataName '.mat'] );
strainTemporal = data.strainTemporal;
bendingTemporal = data.bendingTemporal;

% Maximum strain and bending

num_vert = size(strainTemporal, 1);
max_strain = 0.0;
for i = 1 : num_vert
    new_max_strain = norm( strainTemporal(i, :), Inf );
    if( new_max_strain > max_strain )
        max_strain = new_max_strain;
    end % if
end % for
if (max_strain ~= 0)
    strainTemporal = strainTemporal ./ max_strain;
end % if

max_bending = 0.0;
for i = 1 : num_vert
    new_max_bending = norm( bendingTemporal(i,:), Inf );
    if(new_max_bending > max_bending )
        max_bending = new_max_bending;
    end % if
end % for
if(max_bending ~= 0)
    bendingTemporal = bendingTemporal ./ max_bending;
end % if

maxStrain = max( strainTemporal, [], 2);
maxBending = max( bendingTemporal, [], 2);
averageStrain = mean( strainTemporal, 2 );
averageBending = mean( bendingTemporal, 2 );

%{
entropyStrain = zeros(obj_ref.nV, 1);
entropyBending = zeros(obj_ref.nV, 1);

n = obj_ref.nV;
approx = 40;
for i = 1:obj_ref.nV
    x = strainTemporal(i,:);
    entropyStrain(i, 1) = entropy(x, approx);
    x = bendingTemporal(i,:);
    entropyBending(i, 1) = entropy(x, approx);
end
%}

source = load( ['../../res/' dataName '/' dataName '.mat'] )
[ nV ] = size( source.frames, 2 );
nV = nV / 3;
nF = size( source.topology, 1 );
obj_ref.vertices = toMatrix( source.frames( 1, : ) );
obj_ref.faces = source.topology;
obj_ref.nF = nF;
obj_ref.nV = nV;
boundingSize = bbox( obj_ref.vertices );

neighborhood = kDIJK_NEIGHBORHOOD * boundingSize;

adjDataFile = load( ['../../res/' dataName '/' dataName 'Adj.mat'] );
adjList = adjDataFile.adjList;
adjVertList = adjDataFile.adjVertList;

% AGGREGATE SCALAR FIELDS
jointField = joinScalarFields( abs(averageStrain), abs(averageBending), kALPHA, kBETA );
maxField = joinScalarFields( abs(maxStrain), abs(maxBending), kALPHA, kBETA );

% FEATURE POINTS
output( jointField, 'AVER', neighborhood, adjVertList, kTRESHOLD, kCOLOR_SLIDER, obj_ref, kTYPE );
output( maxField, 'MAX', neighborhood, adjVertList, kTRESHOLD, kCOLOR_SLIDER, obj_ref, kTYPE );

%{
[path, id] = getPathAndId4DataType( kTYPE );
featPoints = featurePoints( averageStrain, neighborhood, adjVertList, id, kTRESHOLD );
nFP = size( featPoints, 2 );
notEmpty = exportFP( featPoints, jointField, path, id);
% END FEATURE POINTS

save( ['../../target/' id '.mat'], 'averageStrain', 'curvChange');

% COLOR::POST PROCESSING
colors = colorForField( jointField, kCOLOR_SLIDER );
pinkColor = [255 0 255];
for i = 1:nFP 
    if ( notEmpty )
        colors( featPoints(1, i), :) = pinkColor;
    end % if
end % FOR
outDir = '../../target/';
path = [outDir id '.ply'];
exportPLY( path, obj_ref.vertices, colors, obj_ref.faces, 0 );
%}
clear; clc;
% END COLOR::POST PROCESSING

%==========================================================================
% END MAIN
% main.m